package com.firmail.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

//@EnableTransactionManagement 在application
//@Configuration
public class JpaConfig /*implements TransactionManagementConfigurer*/ {

    @Autowired
    DsProperties properties;

    @Bean(name = "primaryDataSource")//primary : 英文“主要的”
    @Qualifier("primaryDataSource")//是一个合格者标识--表明那个类才是我们需要调用的类
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")//secondary ： 英文“次要的”
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")//.secondary
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }


    //@Bean
    public DataSource dataSource() {
        //System.out.println("++++++++++++++++++++++++++++++++++++++++++"+driverClassName+"============url "+url);
        System.out.println("++++++++++++++++++++++++++++++++++++++++++"+properties.getDriverClassName()+
                "============url "+properties.getUrl());
        //return DataSourceBuilder.create().type(DruidDataSource.class).build();
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://mysql-server:3306/mock?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true");
        dataSource.setUsername("freddy");
        dataSource.setPassword("2frederica.99z17");
        return dataSource;
    }

    /*@Bean//注意这个bean 事务管理器的名字是 tx ，后面方法用的时候记得加上这个名字
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    }
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("com.firmail.entity");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        Properties properties = new Properties();
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        properties.setProperty("hibernate.c3p0.min_size", "10");
        properties.setProperty("hibernate.c3p0.max_size", "100");
        properties.setProperty("hibernate.jdbc.fetch_size", "100");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        //设置事务提交模式
        //properties.put("hibernate.connection.autocommit", false);
        //properties.put("org.hibernate.flushMode", "COMMIT");
        //properties.put("hibernate.enable_lazy_load_no_trans",true);
        em.setJpaProperties(properties);
        return em;
    }*/

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.firmail.entity");
        factory.setJpaProperties(hibernateProperties());
        factory.setDataSource(primaryDataSource());
        return factory;
    }
    // 创建事务管理器1
    /*@Bean(name = "txManager1")
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Resource(name = "txManager2")
    private PlatformTransactionManager txManager2;*/

    @Bean//(name="txManager2")//注意这个bean 事务管理器的名字是 tx ，后面方法用的时候记得加上这个名字
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        // 显示sql语句
        properties.put("hibernate.show_sql", true);
        // 格式化sql语句
        properties.put("hibernate.format_sql", true);
        // 方言
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        // 自动生成表
        properties.put("hibernate.hbm2ddl.auto", "update");

        properties.setProperty("hibernate.c3p0.min_size", "10");
        properties.setProperty("hibernate.c3p0.max_size", "100");
        properties.setProperty("hibernate.jdbc.fetch_size", "100");
        //设置事务提交模式
//        properties.put("hibernate.connection.autocommit",false);
//        properties.put("org.hibernate.flushMode", "COMMIT");
//        properties.put("hibernate.enable_lazy_load_no_trans",true);
        return properties;
    }

    /*@Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return txManager2;
    }*/
}
